<?php
	include_once "dataservice.php";
	
	class module_db
	{
		public function Them($TenModule, $MoTa, $TenFile, $TrangThai, $Index, $PIN)
		{
			$sql = "INSERT INTO `tbl_module`(`MaModule`, `TenModule`, `MoTa`, `TenFile`, `TrangThai`, `Index`, `PIN`) 
					VALUES ('$TenModule', '$MoTa', '$TenFile', '$TrangThai', '$Index', '$PIN')";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaModule, $TenModule, $MoTa, $TenFile, $TrangThai, $Index, $PIN)
		{
			$sql = "UPDATE 	`tbl_module` 
					SET 	`TenModule` = '$TenModule',
							`MoTa`      = '$MoTa',
							`TenFile`   = '$TenFile',
							`TrangThai` = '$TrangThai',
							`Index`     = '$Index',
							`PIN`       = '$PIN'
					WHERE 	`MaModule`  = '$MaModule'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatPIN($MaModule, $PIN)
		{
			$sql = "UPDATE 	`tbl_module` 
					SET 	`PIN`       = '$PIN'
					WHERE 	`MaModule`  = '$MaModule'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatTrangThai($MaModule, $TrangThai)
		{
			$sql = "UPDATE 	`tbl_module` 
					SET 	`TrangThai`       = '$TrangThai'
					WHERE 	`MaModule`  = '$MaModule'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaModule)
		{
			$sql = "DELETE FROM `tbl_module` WHERE `MaModule` = '$MaModule'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaModule, $TrangThai, $Index)
		{
			$sql = "SELECT * FROM `tbl_module` WHERE 1";
			
			if($MaModule != 0)
				$sql .= " AND `MaModule` = $MaModule";
			if($TrangThai != -1)
				$sql .= " AND `TrangThai` = $TrangThai";
			if($Index != -1)
				$sql .= " AND `Index` = $Index";
			$sql .= " ORDER BY `Index`";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function MaxIndex()
		{
			$sql = "SELECT MAX(`Index`) FROM `tbl_module`";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function MinIndex()
		{
			$sql = "SELECT MIN(`Index`) FROM `tbl_module`";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function CapNhatIndex($MaModule, $Index)
		{
			$sql = "UPDATE 	`tbl_module` 
					SET 	`Index`     = '$Index' 
					WHERE 	`MaModule`  = '$MaModule'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
	}
?>